package com.asia5b.a5bmall.same.dialog

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import com.asia5b.a5bmall.same.R
import com.asia5b.a5bmall.same.adapter.ParcelAddressAdapter
import com.asia5b.a5mall.middleware.model.member.AddressModel
import com.asia5b.a5mall.middleware.base.BaseDialog
import com.asia5b.a5mall.middleware.utils.newDivider
import kotlinx.android.synthetic.main.dialog_parceladdress.*

/**
 * Created by zhangyuncai on 2020/3/26.
 */
class ParcelAddressDialog constructor(context: Context) : BaseDialog(context) {
    override val layoutId: Int=R.layout.dialog_parceladdress
    override val heightPercent: Double=0.7

    val mDatas = arrayListOf<AddressModel>()
    val mAdapter = ParcelAddressAdapter(mDatas)
    val mRecyclerView by lazy { rv_parcel }

    var selectAddress: AddressModel? = null



    override fun initView() {
        mAdapter.selectId = selectAddress?.addr_id ?: 0
        mRecyclerView.run {
            layoutManager = LinearLayoutManager(context)!!
            adapter = mAdapter
            addItemDecoration(newDivider(context))
        }
    }

    override fun initEvent() {
        mAdapter.setOnItemClickListener { _, _, position ->
            val addressModel = mAdapter.getItem(position) as AddressModel
            val selectId = selectAddress?.addr_id ?: 0
            if (selectId != addressModel.addr_id) {
                mSureClick?.invoke(addressModel)
            }
            dismiss()
        }
        iv_dialog_close.setOnClickListener {
            mCancelClick?.invoke()
            dismiss()
        }
    }



}